CS502 Computing and Communication Technology October 13, 2015
Homework 3
There are five programming questions for you to do. Please submit five .s or .asm files. In the
settings on SPIM for the first 4 questions, as usual use a bare machine with no exception handler,
and also choose “Enable Delayed Loads” and “Enable Delayed Branches”. This simulates a more
realistic machine with a pipelined architecture.
1. (20 points) Sum of Odds
Write a program that computes the sum of all the odd numbers between 1 and 99:
1 + 3 + 5 + 7 + · · · + 97 + 99
Do this with one counting loop. The loop just needs to increment the counter by the proper
amount on each iteration.
2. (20 points) Significant Bits
Write a program that loads a word into register $8, and then computes the answer to the
following question: How many significant bits are in the number in register $8?
The significant bits in a number are the leftmost 1 and all bits to its right. So the bit pattern:
0000 0000 0010 1001 1000 1101 0111 1101
… has 22 significant bits. (For example, to load register $8 with the above pattern, 0x00298D7D,
use an ori followed by a left shift followed by another ori.)
Do this with one loop. Your program should leave the result in register $5.
3. (20 points) Allowed Ranges
A temperature in $8 is allowed to be within either of two ranges: 20 = temp = 40 and 60 =
temp = 80. Write a program that sets a flag (register $3) to 1 if the temperature is in an allowed
range and to 0 if the temperature is not in an allowed range.
4. (20 points) To Lower Case
Declare a string in the data section:
. data
string : . asciiz ” ABCDEFG ”
Write a program that converts the string to all lower case characters. Do this by adding 0x20
to each character in the string. (See the ASCII chart to figure out why this works.)
Assume that the data consists only of uppercase alphabetical characters, with no spaces or
punctuation.
1
For the 5th question, in the Settings menu of SPIM set Bare Machine OFF, Allow Pseudo
Instructions ON, Exception Handler OFF, Delayed Branches ON, Delayed Loads ON and
Mapped IO OFF.
Use the la, lw and sw pseudo-instructions with symbolic addresses.
5. (20 points) Pair-wise Addition
Declare three arrays, each of the same size:
. data
size . word 7
array1 : . word -30 , -23 , 56 , -43 , 72 , -18 , 71
array2 : . word 45 , 23 , 21 , -23 , -82 , 0 , 69
result : . word 0, 0, 0, 0, 0 , 0 , 0
Initialize a base register for each array (use the la instruction.) Now implement a loop that
adds corresponding elements in the first two arrays and stores the result in the corresponding
element of the result array.
2
CS502 Computing and Communication Technology October 13, 2015
CS502 Computing and Communication Technology October 13, 2015
CS502 Computing and Communication Technology October 13, 2015
Homework 3
There are five programming questions for you to do. Please submit five .s or .asm files. In the
settings on SPIM for the first 4 questions, as usual use a bare machine with no exception handler,
and also choose “Enable Delayed Loads” and “Enable Delayed Branches”. This simulates a more
realistic machine with a pipelined architecture.
1. (20 points) Sum of Odds
Write a program that computes the sum of all the odd numbers between 1 and 99:
1 + 3 + 5 + 7 + · · · + 97 + 99
Do this with one counting loop. The loop just needs to increment the counter by the proper
amount on each iteration.
2. (20 points) Significant Bits
Write a program that loads a word into register $8, and then computes the answer to the
following question: How many significant bits are in the number in register $8?
The significant bits in a number are the leftmost 1 and all bits to its right. So the bit pattern:
0000 0000 0010 1001 1000 1101 0111 1101
… has 22 significant bits. (For example, to load register $8 with the above pattern, 0x00298D7D,
use an ori followed by a left shift followed by another ori.)
Do this with one loop. Your program should leave the result in register $5.
3. (20 points) Allowed Ranges
A temperature in $8 is allowed to be within either of two ranges: 20 = temp = 40 and 60 =
temp = 80. Write a program that sets a flag (register $3) to 1 if the temperature is in an allowed
range and to 0 if the temperature is not in an allowed range.
4. (20 points) To Lower Case
Declare a string in the data section:
. data
string : . asciiz ” ABCDEFG ”
Write a program that converts the string to all lower case characters. Do this by adding 0x20
to each character in the string. (See the ASCII chart to figure out why this works.)
Assume that the data consists only of uppercase alphabetical characters, with no spaces or
punctuation.
1
For the 5th question, in the Settings menu of SPIM set Bare Machine OFF, Allow Pseudo
Instructions ON, Exception Handler OFF, Delayed Branches ON, Delayed Loads ON and
Mapped IO OFF.
Use the la, lw and sw pseudo-instructions with symbolic addresses.
5. (20 points) Pair-wise Addition
Declare three arrays, each of the same size:
. data
size . word 7
array1 : . word -30 , -23 , 56 , -43 , 72 , -18 , 71
array2 : . word 45 , 23 , 21 , -23 , -82 , 0 , 69
result : . word 0, 0, 0, 0, 0 , 0 , 0
Initialize a base register for each array (use the la instruction.) Now implement a loop that
adds corresponding elements in the first two arrays and stores the result in the corresponding
element of the result array.
2